﻿using Dm.util;
using SqlSugar;
namespace DataCollection.Core.Models
{
    #region 属性
    /// <summary>
    /// 设备点位
    ///</summary>
    [SugarTable("c_device_point")]
    public class DevicePointModel
    {
        /// <summary>
        /// 备  注:点位编号
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "number", IsPrimaryKey = true)]
        public long Number { get; set; }

        /// <summary>
        /// 备  注:点位名称
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "name")]
        public string Name { get; set; } = null!;

        /// <summary>
        /// 备  注:点位地址
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "address")]
        public int Address { get; set; }

        /// <summary>
        /// 备  注:点位类型
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "point_type")]
        public string PointType { get; set; } = null!;

        /// <summary>
        /// 备  注:量程最小值
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "range_min")]
        public double RangeMin { get; set; }

        /// <summary>
        /// 备  注:量程最大值
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "range_max")]
        public double RangeMax { get; set; }

        /// <summary>
        /// 备  注:输出最小值
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "out_min")]
        public double OutMin { get; set; }

        /// <summary>
        /// 备  注:输出最大值
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "out_max")]
        public double OutMax { get; set; }

        /// <summary>
        /// 备  注:修正值
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "amend")]
        public double Amend { get; set; }

        /// <summary>
        /// 备  注:设备编号
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "device_number")]
        public long DeviceNumber { get; set; }

        /// <summary>
        /// 备  注:设备名称
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "device_name")]
        public string DeviceName { get; set; } = null!;

        /// <summary>
        /// 备  注:设备地址
        /// 默认值:
        ///</summary>
        [SugarColumn(ColumnName = "device_address")]
        public string DeviceAddress { get; set; } = null!;

        //[Navigate(NavigateType.OneToOne, nameof(Number), nameof(FormPointModel.DevicePointNumber))]
        //public List<FormPointModel>? Points { get; set; }

        [Navigate(NavigateType.OneToOne, nameof(Number), nameof(FormPointModel.DevicePointNumber))]
        public FormPointModel? PointModel { get; set; }

        #endregion

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public int Add()
        {
            List<DevicePointModel> list = new List<DevicePointModel>();
            list.Add(this);
            int res = SqlSugarHelper.Insertable<DevicePointModel>(list);
            return res;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public int Delete()
        {
            List<DevicePointModel> list = new List<DevicePointModel>();
            list.Add(this);
            int res = SqlSugarHelper.Deleteable<DevicePointModel>(list);
            return res;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <returns></returns>
        public int Update()
        {
            List<DevicePointModel> list = new List<DevicePointModel>();
            list.Add(this);
            int res = SqlSugarHelper.Updateable<DevicePointModel>(list);
            return res;
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>

        public static List<DevicePointModel> List(Dictionary<string, object> where)
        {
            ISugarQueryable<DevicePointModel> sqlQueryable = SqlSugarHelper.DB.Queryable<DevicePointModel>().WhereColumns(where);
            return sqlQueryable.OrderBy((d) => d.Address, OrderByType.Asc).OrderBy((d) => d.PointType, OrderByType.Asc).Includes(x => x.PointModel).ToList();
        }
    }

}
